package com.itheima.Service.Impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.itheima.Service.ReportService;
import com.itheima.mapper.ReportDao;
import com.itheima.pojo.*;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URL;
import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.*;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;


    @Override
    public MemberReport getMemberReport() {
        LocalDate currentDate = LocalDate.now();
        MemberReport memberReport = new MemberReport();

        // 获取当前月份的前一个月
        LocalDate startMonth = currentDate.minusMonths(1);

        // 获取去年12月的最后一天
        LocalDate endMonth = LocalDate.of(currentDate.getYear() - 1, 12, 31);

        // 从上个月开始到去年12月的每个月最后一天
        while (!startMonth.isBefore(endMonth)) {
            // 获取当前月的最后一天
            LocalDate lastDayOfMonth = startMonth.withDayOfMonth(startMonth.lengthOfMonth());
            memberReport.getMonths().add(lastDayOfMonth);

            // 向上滚动到上一个月
            startMonth = startMonth.minusMonths(1);
        }

        Collections.reverse(memberReport.getMonths());

        // 获取每个月的会员数量
        for (LocalDate month : memberReport.getMonths()) {
            memberReport.getMemberCount().add(reportDao.selectMemNumByMonth(month));
        }

        return memberReport;
    }

    @Override
    public SetMealData getSetmealReport() {
        //统计预约套餐数量
        List<SetmealCount> setmealCount = reportDao.selectSetmealReport();

        SetMealData setMealData = new SetMealData();
        for (SetmealCount count : setmealCount) {
            setMealData.getSetmealNames().add(count.getName());
        }

        setMealData.setSetmealCount(setmealCount);

        return setMealData;

    }

    @Override
    public ReportData getBusinessReportData() {
        LocalDate now = LocalDate.now();
        ReportData reportData = new ReportData();
        reportData.setReportDate(now);
        //获得本周一的时间
        LocalDate thisWeek = now.with(DayOfWeek.MONDAY);
        //获得这个月的时间
        LocalDate thisMonth = now.withDayOfMonth(1);
        //今日到诊人数
        reportData.setTodayVisitsNumber(reportDao.selectVisitsNumByDate("now", now));
        //本周到诊人数
        reportData.setThisWeekVisitsNumber(reportDao.selectVisitsNumByDate("thisWeek", thisWeek));
        //本月到诊人数
        reportData.setThisMonthVisitsNumber(reportDao.selectVisitsNumByDate("thisMonth", thisMonth));
        //总预约数
        int totalNum = reportDao.selectTotalNum();
        //排名前四的套餐
        List<HotSetmeal> hotSetmeal = reportDao.selectHotSetmeal();
        //打印出hotSetmeal数据
        for (HotSetmeal setmeal : hotSetmeal) {
            System.out.println(setmeal.getSetmeal_count());
        }
        for (HotSetmeal setmeal : hotSetmeal) {
            setmeal.setProportion(setmeal.getSetmeal_count() * 1.0 / totalNum);
        }
        reportData.setHotSetmeal(hotSetmeal);
        //本日新增会员数
        reportData.setTodayNewMember(reportDao.selectNewMemberByDate("now", now));
        //本周新增会员数
        reportData.setThisWeekNewMember(reportDao.selectNewMemberByDate("thisWeek", thisWeek));
        //本月新增会员数
        reportData.setThisMonthNewMember(reportDao.selectNewMemberByDate("thisMonth", thisMonth));
        //本周预约数
        reportData.setThisWeekOrderNumber(reportDao.selectOrderNumByDate("thisWeek", thisWeek));
        //本日预约数
        reportData.setTodayOrderNumber(reportDao.selectOrderNumByDate("now", now));
        //本月预约数
        reportData.setThisMonthOrderNumber(reportDao.selectOrderNumByDate("thisMonth", thisMonth));
        return reportData;


    }

    @Override
    public void exportBusinessReport(HttpServletResponse response) throws IOException {
        // 1. 获取数据
        ReportData reportData = getBusinessReportData();

        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("templates/report_template.xlsx");

        response.setContentType("application/vnd.ms-excel"); // 或根据实际文件类型设置
        response.setHeader("Content-Disposition", "attachment;filename=orderSetting.xls");

        XSSFWorkbook excel = new XSSFWorkbook(inputStream);

        //获得第一页
        XSSFSheet sheet = excel.getSheetAt(0);

        //获得第二行
        XSSFRow row = sheet.getRow(2);
        //获得第三列
        XSSFCell cell = row.getCell(5);
        cell.setCellValue(reportData.getReportDate().toString());
        //获得第五行
        row = sheet.getRow(5);
        cell = row.getCell(5);
        cell.setCellValue(reportData.getTodayNewMember());
        cell = row.getCell(7);
        cell.setCellValue(reportData.getTotalMember());
        row = sheet.getRow(6);
        cell = row.getCell(5);
        cell.setCellValue(reportData.getThisWeekNewMember());
        cell = row.getCell(7);
        cell.setCellValue(reportData.getThisMonthNewMember());
        sheet.getRow(8).getCell(5).setCellValue(reportData.getTodayOrderNumber());
        sheet.getRow(8).getCell(7).setCellValue(reportData.getTodayVisitsNumber());
        sheet.getRow(9).getCell(5).setCellValue(reportData.getThisWeekOrderNumber());
        sheet.getRow(9).getCell(7).setCellValue(reportData.getThisWeekVisitsNumber());
        sheet.getRow(10).getCell(5).setCellValue(reportData.getThisMonthOrderNumber());
        sheet.getRow(10).getCell(7).setCellValue(reportData.getThisMonthVisitsNumber());
        int rowNum = 12; // 起始行号
        for (HotSetmeal hotSetmeal : reportData.getHotSetmeal()) {
            sheet.getRow(rowNum).getCell(4).setCellValue(hotSetmeal.getName()); // 第一列
            sheet.getRow(rowNum).getCell(5).setCellValue(hotSetmeal.getSetmeal_count()); // 第二列
            sheet.getRow(rowNum).getCell(6).setCellValue(hotSetmeal.getProportion()); // 第三列

            rowNum++; // 移动到下一行
        }

        OutputStream outputStream = response.getOutputStream();
        excel.write(outputStream);

        // 关闭流
        outputStream.close();
        excel.close();

    }







}


